home *** CD-ROM | disk | FTP | other *** search
/ Clickx 115 / Clickx 115.iso / software / tools / windows / tails-i386-0.16.iso / live / filesystem.squashfs / usr / sbin / invoke-rc.d < prev    next >
Encoding:
Text File  |  2012-03-26  |  11.0 KB  |  465 lines

  1. #!/bin/sh  
  2. #
  3. # invoke-rc.d.sysvinit - Executes initscript actions
  4. #
  5. # SysVinit /etc/rc?.d version for Debian's sysvinit package
  6. #
  7. # Copyright (C) 2000,2001 Henrique de Moraes Holschuh <hmh@debian.org>
  8. #
  9. # This program is free software; you can redistribute it and/or modify it
  10. # under the terms of the GNU General Public License as published by the Free
  11. # Software Foundation; either version 2 of the License, or (at your option)
  12. # any later version.
  13. #
  14. # This program is distributed in the hope that it will be useful, but
  15. # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  16. # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  17. # for more details.
  18. #
  19. # You should have received a copy of the GNU General Public License along
  20. # with this program; if not, write to the Free Software Foundation, Inc.,
  21. # 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
  22.  
  23. # Constants
  24. RUNLEVEL=/sbin/runlevel
  25. POLICYHELPER=/usr/sbin/policy-rc.d
  26. INITDPREFIX=/etc/init.d/
  27. RCDPREFIX=/etc/rc
  28.  
  29. # Options
  30. BEQUIET=
  31. MODE=
  32. ACTION=
  33. FALLBACK=
  34. NOFALLBACK=
  35. FORCE=
  36. RETRY=
  37. RETURNFAILURE=
  38. RC=
  39.  
  40. # Shell options
  41. set +e
  42.  
  43. dohelp () {
  44.  # 
  45.  # outputs help and usage
  46.  #
  47. cat <<EOF
  48.  
  49. invoke-rc.d, Debian/SysVinit (/etc/rc?.d) initscript subsystem.
  50. Copyright (c) 2000,2001 Henrique de Moraes Holschuh <hmh@debian.org>
  51.  
  52. Usage:
  53.   invoke-rc.d [options] <basename> <action> [extra parameters]
  54.  
  55.   basename - Initscript ID, as per update-rc.d(8)
  56.   action   - Initscript action. Known actions are:
  57.                 start, [force-]stop, restart,
  58.                 [force-]reload, status
  59.   WARNING: not all initscripts implement all of the above actions.
  60.  
  61.   extra parameters are passed as is to the initscript, following 
  62.   the action (first initscript parameter).
  63.  
  64. Options:
  65.   --quiet
  66.      Quiet mode, no error messages are generated.
  67.   --force
  68.      Try to run the initscript regardless of policy and subsystem
  69.      non-fatal errors.
  70.   --try-anyway
  71.      Try to run init script even if a non-fatal error is found.
  72.   --disclose-deny
  73.      Return status code 101 instead of status code 0 if
  74.      initscript action is denied by local policy rules or
  75.      runlevel constrains.
  76.   --query
  77.      Returns one of status codes 100-106, does not run
  78.      the initscript. Implies --disclose-deny and --no-fallback.
  79.   --no-fallback
  80.      Ignores any fallback action requests by the policy layer.
  81.      Warning: this is usually a very *bad* idea for any actions
  82.      other than "start".
  83.   --help
  84.      Outputs help message to stdout
  85.  
  86. EOF
  87. }
  88.  
  89. printerror () {
  90.  #
  91.  # prints an error message
  92.  #  $* - error message
  93.  #
  94. if test x${BEQUIET} = x ; then
  95.     echo `basename $0`: "$*" >&2
  96. fi
  97. }
  98.  
  99. formataction () {
  100.  #
  101.  # formats a list in $* into $printaction
  102.  # for human-friendly printing to stderr
  103.  # and sets $naction to action or actions
  104.  #
  105. printaction=`echo $* | sed 's/ /, /g'`
  106. if test $# -eq 1 ; then
  107.     naction=action
  108. else
  109.     naction=actions
  110. fi
  111. }
  112.  
  113. querypolicy () {
  114.  #
  115.  # queries policy database
  116.  # returns: $RC = 104 - ok, run
  117.  #          $RC = 101 - ok, do not run
  118.  #        other - exit with status $RC, maybe run if $RETRY
  119.  #          initial status of $RC is taken into account.
  120.  #
  121.  
  122. policyaction="${ACTION}"
  123. if test x${RC} = "x101" ; then
  124.     if test "${ACTION}" = "start" || test "${ACTION}" = "restart" ; then
  125.     policyaction="(${ACTION})"
  126.     fi
  127. fi
  128.  
  129. if test "x${POLICYHELPER}" != x && test -x "${POLICYHELPER}" ; then
  130.     FALLBACK=`${POLICYHELPER} ${BEQUIET} ${INITSCRIPTID} "${policyaction}" ${RL}`
  131.     RC=$?
  132.     formataction ${ACTION}
  133.     case ${RC} in
  134.     0)   RC=104
  135.          ;;
  136.     1)   RC=105
  137.          ;;
  138.     101) if test x${FORCE} != x ; then
  139.         printerror Overriding policy-rc.d denied execution of ${printaction}.
  140.         RC=104
  141.          else
  142.         printerror policy-rc.d denied execution of ${printaction}.
  143.          fi
  144.          ;;
  145.     esac
  146.     if test x${MODE} != xquery ; then
  147.       case ${RC} in
  148.     105) printerror policy-rc.d query returned \"behaviour undefined\",
  149.          printerror assuming \"${printaction}\" is allowed.
  150.          RC=104
  151.          ;;
  152.     106) formataction ${FALLBACK}
  153.          if test x${FORCE} = x ; then
  154.          if test x${NOFALLBACK} = x ; then
  155.              ACTION="${FALLBACK}"
  156.              printerror executing ${naction} \"${printaction}\" instead due to policy-rc.d request.
  157.              RC=104
  158.          else
  159.              printerror ignoring policy-rc.d fallback request: ${printaction}.
  160.              RC=101
  161.          fi
  162.          else
  163.          printerror ignoring policy-rc.d fallback request: ${printaction}.
  164.          RC=104
  165.          fi
  166.          ;;
  167.       esac
  168.     fi
  169.     case ${RC} in
  170.       100|101|102|103|104|105|106) ;;
  171.       *) printerror WARNING: policy-rc.d returned unexpected error status ${RC}, 102 used instead.
  172.          RC=102
  173.      ;;
  174.     esac
  175. else
  176.     if test x${RC} = x ; then 
  177.     RC=104
  178.     fi
  179. fi
  180. return
  181. }
  182.  
  183. verifyparameter () {
  184.  #
  185.  # Verifies if $1 is not null, and $# = 1
  186.  #
  187. if test $# -eq 0 ; then
  188.     printerror syntax error: invalid empty parameter
  189.     exit 103
  190. elif test $# -ne 1 ; then
  191.     printerror syntax error: embedded blanks are not allowed in \"$*\"
  192.     exit 103
  193. fi
  194. return
  195. }
  196.  
  197. ##
  198. ##  main
  199. ##
  200.  
  201. ## Verifies command line arguments
  202.  
  203. if test $# -eq 0 ; then
  204.   printerror syntax error: missing required parameter, --help assumed
  205.   dohelp
  206.   exit 103
  207. fi
  208.  
  209. state=I
  210. while test $# -gt 0 && test ${state} != III ; do
  211.     case "$1" in
  212.       --help)   dohelp 
  213.         exit 0
  214.         ;;
  215.       --quiet)  BEQUIET=--quiet
  216.         ;;
  217.       --force)  FORCE=yes
  218.         RETRY=yes
  219.         ;;
  220.       --try-anyway)
  221.             RETRY=yes
  222.         ;;
  223.       --disclose-deny)
  224.         RETURNFAILURE=yes
  225.         ;;
  226.       --query)  MODE=query
  227.         RETURNFAILURE=yes
  228.         ;;
  229.       --no-fallback)
  230.         NOFALLBACK=yes
  231.         ;;
  232.       --*)    printerror syntax error: unknown option \"$1\"
  233.         exit 103
  234.         ;;
  235.     *)      case ${state} in
  236.         I)  verifyparameter $1
  237.             INITSCRIPTID=$1
  238.             ;;
  239.         II) verifyparameter $1
  240.             ACTION=$1
  241.             ;;
  242.         esac
  243.         state=${state}I
  244.         ;;
  245.     esac
  246.     shift
  247. done
  248.  
  249. if test ${state} != III ; then
  250.     printerror syntax error: missing required parameter
  251.     exit 103
  252. fi
  253.  
  254. #NOTE: It may not be obvious, but "$@" from this point on must expand
  255. #to the extra initscript parameters, except inside functions.
  256.  
  257. ## sanity checks and just-in-case warnings.
  258. case ${ACTION} in
  259.     start|stop|force-stop|restart|reload|force-reload|status)
  260.     ;;
  261.     *)
  262.     if test "x${POLICYHELPER}" != x && test -x "${POLICYHELPER}" ; then
  263.         printerror action ${ACTION} is unknown, but proceeding anyway.
  264.     fi
  265.     ;;
  266. esac
  267.  
  268. ## Verifies if the given initscript ID is known
  269. ## For sysvinit, this error is critical
  270. if test ! -f "${INITDPREFIX}${INITSCRIPTID}" ; then
  271.     printerror unknown initscript, ${INITDPREFIX}${INITSCRIPTID} not found.
  272.     exit 100
  273. fi
  274.  
  275. ## Queries sysvinit for the current runlevel
  276. RL=`${RUNLEVEL} | sed 's/.*\ //'`
  277. if test ! $? ; then
  278.     printerror "could not determine current runlevel"
  279.     if test x${RETRY} = x ; then
  280.     exit 102
  281.     fi
  282.     RL=
  283. fi
  284.  
  285. ## Running ${RUNLEVEL} to get current runlevel do not work in the boot
  286. ## runlevel (scripts in /etc/rcS.d/), as /var/run/utmp contain
  287. ## runlevel 0 or 6 (written at shutdown) at that point.
  288. if test x${RL} = x0 || test x${RL} = x6 ; then
  289.     if ps -fp 1 | grep -q 'init boot' ; then
  290.        RL=S
  291.     fi
  292. fi
  293.  
  294. ## Handles shutdown sequences VERY safely
  295. ## i.e.: forget about policy, and do all we can to run the script.
  296. ## BTW, why the heck are we being run in a shutdown runlevel?!
  297. if test x${RL} = x0 || test x${RL} = x6 ; then
  298.     FORCE=yes
  299.     RETRY=yes
  300.     POLICYHELPER=
  301.     BEQUIET=
  302.     printerror "-----------------------------------------------------"
  303.     printerror "WARNING: 'invoke-rc.d ${INITSCRIPTID} ${ACTION}' called"
  304.     printerror "during shutdown sequence."
  305.     printerror "enabling safe mode: initscript policy layer disabled"
  306.     printerror "-----------------------------------------------------"
  307. fi
  308.  
  309. ## Verifies the existance of proper S??initscriptID and K??initscriptID 
  310. ## *links* in the proper /etc/rc?.d/ directory
  311. verifyrclink () {
  312.   #
  313.   # verifies if parameters are non-dangling symlinks
  314.   # all parameters are verified
  315.   #
  316.   doexit=
  317.   while test $# -gt 0 ; do
  318.     if test ! -L "$1" ; then
  319.         printerror not a symlink: $1
  320.         doexit=102
  321.     fi
  322.     if test ! -f "$1" ; then
  323.         printerror dangling symlink: $1
  324.         doexit=102
  325.     fi
  326.     shift
  327.   done
  328.   if test x${doexit} != x && test x${RETRY} = x; then
  329.      exit ${doexit}
  330.   fi
  331.   return 0
  332. }
  333.  
  334. # we do handle multiple links per runlevel
  335. # but we don't handle embedded blanks in link names :-(
  336. if test x${RL} != x ; then
  337.     SLINK=`ls -d -Q ${RCDPREFIX}${RL}.d/S[0-9][0-9]${INITSCRIPTID} 2>/dev/null | xargs`
  338.     KLINK=`ls -d -Q ${RCDPREFIX}${RL}.d/K[0-9][0-9]${INITSCRIPTID} 2>/dev/null | xargs`
  339.     SSLINK=`ls -d -Q ${RCDPREFIX}S.d/S[0-9][0-9]${INITSCRIPTID} 2>/dev/null | xargs`
  340.  
  341.     verifyrclink ${SLINK} ${KLINK} ${SSLINK}
  342. fi
  343.  
  344. testexec () {
  345.   #
  346.   # returns true if any of the parameters is
  347.   # executable (after following links)
  348.   #
  349.   while test $# -gt 0 ; do
  350.     if test -x "$1" ; then
  351.        return 0
  352.     fi
  353.     shift
  354.   done
  355.   return 1
  356. }
  357.  
  358. RC=
  359.  
  360. ###
  361. ### LOCAL INITSCRIPT POLICY: Enforce need of a start entry
  362. ### in either runlevel S or current runlevel to allow start
  363. ### or restart.
  364. ###
  365. case ${ACTION} in
  366.   start|restart)
  367.     if testexec ${SLINK} ; then
  368.     RC=104
  369.     elif testexec ${KLINK} ; then
  370.     RC=101
  371.     elif testexec ${SSLINK} ; then
  372.     RC=104
  373.     fi
  374.   ;;
  375. esac
  376.  
  377. # test if /etc/init.d/initscript is actually executable
  378. if testexec "${INITDPREFIX}${INITSCRIPTID}" ; then
  379.     if test x${RC} = x && test x${MODE} = xquery ; then
  380.         RC=105
  381.     fi
  382.  
  383.     # call policy layer
  384.     querypolicy
  385.     case ${RC} in
  386.         101|104)
  387.            ;;
  388.         *) if test x${MODE} != xquery ; then
  389.            printerror policy-rc.d returned error status ${RC}
  390.            if test x${RETRY} = x ; then
  391.                exit ${RC}
  392.                else
  393.                    RC=102
  394.                fi
  395.            fi
  396.            ;;
  397.     esac
  398. else
  399.     ###
  400.     ### LOCAL INITSCRIPT POLICY: non-executable initscript; deny exec.
  401.     ### (this is common sense, actually :^P )
  402.     ###
  403.     RC=101
  404. fi
  405.  
  406. ## Handles --query
  407. if test x${MODE} = xquery ; then
  408.     exit ${RC}
  409. fi
  410.  
  411.  
  412. setechoactions () {
  413.     if test $# -gt 1 ; then
  414.     echoaction=true
  415.     else
  416.     echoaction=
  417.     fi
  418. }
  419. getnextaction () {
  420.     saction=$1
  421.     shift
  422.     ACTION="$@"
  423. }
  424.  
  425. ## Executes initscript
  426. ## note that $ACTION is a space-separated list of actions
  427. ## to be attempted in order until one suceeds.
  428. if test x${FORCE} != x || test ${RC} -eq 104 ; then
  429.     if testexec "${INITDPREFIX}${INITSCRIPTID}" ; then
  430.     RC=102
  431.     setechoactions ${ACTION}
  432.     while test ! -z "${ACTION}" ; do
  433.         getnextaction ${ACTION}
  434.         if test ! -z ${echoaction} ; then
  435.         printerror executing initscript action \"${saction}\"...
  436.         fi
  437.  
  438.         "${INITDPREFIX}${INITSCRIPTID}" "${saction}" "$@" && exit 0
  439.         RC=$?
  440.  
  441.         if test ! -z "${ACTION}" ; then
  442.         printerror action \"${saction}\" failed, trying next action...
  443.         fi
  444.     done
  445.     printerror initscript ${INITSCRIPTID}, action \"${saction}\" failed.
  446.     exit ${RC}
  447.     fi
  448.     exit 102
  449. fi
  450.  
  451. ## Handles --disclose-deny and denied "status" action (bug #381497)
  452. if test ${RC} -eq 101 && test x${RETURNFAILURE} = x ; then
  453.     if test "x${ACTION%% *}" = "xstatus"; then
  454.     printerror emulating initscript action \"status\", returning \"unknown\"
  455.     RC=4
  456.     else
  457.         RC=0
  458.     fi
  459. else
  460.     formataction ${ACTION}
  461.     printerror initscript ${naction} \"${printaction}\" not executed.
  462. fi
  463.  
  464. exit ${RC}
  465.